package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.Date;

import com.acme.bankapp.service.bank.ClientRegistrationListener;

public class Bank implements Serializable {

	private static final long serialVersionUID = 9149769900608852905L;
	private static final String CLIENT_ALREADY_EXIST = "Client with this name already exist: ";
	private Client[] clients;
	private ClientRegistrationListener[] listeners = new ClientRegistrationListener[3];

	public Bank(Client[] clients, ClientRegistrationListener[] listeners) throws ClientExistsException {
		this.listeners = listeners;

		for (Client client : clients) {
			this.addClient(client);
		}
	}

	
	public Bank() {
	}

	public Client[] getClients() {
		return clients;
	}

	public Bank(ClientRegistrationListener[] listeners){
		this.listeners = listeners;
	}
	
	public void addClient(Client client) throws ClientExistsException {
		if (this.clients == null) {
			this.clients = new Client[] { client };
		} else {
			for (Client c : clients) {
				if (c.getName().equals(client.getName())) {
					throw new ClientExistsException(CLIENT_ALREADY_EXIST + c.getClientSalutation());
				}
			}
			Client[] newClientArray = new Client[clients.length + 1];
			System.arraycopy(clients, 0, newClientArray, 0, clients.length);
			newClientArray[newClientArray.length - 1] = client;
			this.clients = newClientArray;
		}

		for (ClientRegistrationListener clientRegistrationListenerclient : listeners) {
			clientRegistrationListenerclient.onClientAdded(client);
		}
		System.out.println();
	}

	public static class DebugListener implements ClientRegistrationListener {
		private static final long serialVersionUID = -2621458764033612958L;

		@Override
		public void onClientAdded(Client c) {
			System.out.println(c.getClientSalutation() + " " + new Date());
		}
	}

	public static class EmailNotificationListener implements ClientRegistrationListener {
		private static final long serialVersionUID = -2605579512784544817L;

		@Override
		public void onClientAdded(Client c) {
			System.out.println("Notification email for client " + c.getClientSalutation() + " to be sent");
		}
	}

	public static class PrintClientListener implements ClientRegistrationListener {
		private static final long serialVersionUID = -4663017543377545949L;

		@Override
		public void onClientAdded(Client c) {
			System.out.println(c.getClientSalutation());
		}
	}
}
